package com.vortex.cloud.sample.easyexcel.nange.readexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-07 周四
 * @time 17:12
 * @description
 */
public class EasyExcelTest {
    public static void main(String[] args) {
        //读取文件
        //创建ExcelReaderBuilder 实例
        ExcelReaderBuilder readerBuilder = EasyExcel.read();
        //获取文件对象
        readerBuilder.file("G:\\easyexcel\\用户数据表.xls");
        //指定sheet
        readerBuilder.sheet("用户数据表");
        //自动关闭输入流
        readerBuilder.autoCloseStream(true);
        //设置Excel 文件格式
        readerBuilder.excelType(ExcelTypeEnum.XLS);
        //注册监听器进行数据的解析
        readerBuilder.registerReadListener(new AnalysisEventListener<Map<Integer, String>>() {

            @Override
            public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                Set<Integer> keySet = integerStringMap.keySet();
                Iterator<Integer> iterator = keySet.iterator();
                while (iterator.hasNext()) {
                Integer key = iterator.next();
                System.out.print(key + ":" + integerStringMap.get(key) + ", ");
            }
                System.out.println("");
        }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                //通知文件读取完毕
                System.out.println("文件读取完毕!!!");
            }


        });
        //构建读取器
        ExcelReader reader = readerBuilder.build();
        //读取数据
        reader.readAll();
        //读取完毕
        reader.finish();

    }
}
